lause. if ( is_array( $this->query_vars['domain__not_in'] ) ) { $this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )"; } if ( ! empty( $this->query_vars['path'] ) ) { $this->sql_clauses['where']['path'] = $wpdb->prepare( 'path = %s', $this->query_vars['path'] ); } // Parse site path for an IN clause. if ( is_array( $this->query_vars['path__in'] ) ) { $this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )"; } // Parse site path for a NOT IN clause. if ( is_array( $this->query_vars['path__not_in'] ) ) { $this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )"; } if ( is_numeric( $this->query_vars['archived'] ) ) { $archived = absint( $this->query_vars['archived'] ); $this->sql_clauses['where']['archived'] = $wpdb->prepare( 'archived = %s ', absint( $archived ) ); } if ( is_numeric( $this->query_vars['mature'] ) ) { $mature = absint( $this->query_vars['mature'] ); $this->sql_clauses['where']['mature'] = $wpdb->prepare( 'mature = %d ', $mature ); } if ( is_numeric( $this->query_vars['spam'] ) ) { $spam = absint( $this->query_vars['spam'] ); $this->sql_clauses['where']['spam'] = $wpdb->prepare( 'spam = %d ', $spam ); } if ( is_numeric( $this->query_vars['deleted'] ) ) { $deleted = absint( $this->query_vars['deleted'] ); $this->sql_clauses['where']['deleted'] = $wpdb->prepare( 'deleted = %d ', $deleted ); } if ( is_numeric( $this->query_vars['public'] ) ) { $public = absint( $this->query_vars['public'] ); $this->sql_clauses['where']['public'] = $wpdb->prepare( 'public = %d ', $public ); } if ( is_numeric( $this->query_vars['lang_id'] ) ) { $lang_id = absint( $this->query_vars['lang_id'] ); $this->sql_clauses['where']['lang_id'] = $wpdb->prepare( 'lang_id = %d ', $lang_id ); } // Parse site language IDs for an IN clause. if ( ! empty( $this->query_vars['lang__in'] ) ) { $this->sql_clauses['where']['lang__in'] = 'lang_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['lang__in'] ) ) . ' )'; } // Parse site language IDs for a NOT IN clause. if ( ! empty( $this->query_vars['lang__not_in'] ) ) { $this->sql_clauses['where']['lang__not_in'] = 'lang_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['lang__not_in'] ) ) . ' )'; } // Falsey search strings are ignored. if ( strlen( $this->query_vars['search'] ) ) { $search_columns = array(); if ( $this->query_vars['search_columns'] ) { $search_columns = array_intersect( $this->query_vars['search_columns'], array( 'domain', 'path' ) ); } if ( ! $search_columns ) { $search_columns = array( 'domain', 'path' ); } /** * Filters the columns to search in a WP_Site_Query search. * * The default columns include 'domain' and 'path. * * @since 4.6.0 * * @param string[] $search_columns Array of column names to be searched. * @param string $search Text being searched. * @param WP_Site_Query $query The current WP_Site_Query instance. */ $search_columns = apply_filters( 'site_search_columns', $search_columns, $this->query_vars['search'], $this ); $this->sql_clauses['where']['search'] = $this->get_search_sql( $this->query_vars['search'], $search_columns ); } $date_query = $this->query_vars['date_query']; if ( ! empty( $date_query ) && is_array( $date_query ) ) { $this->date_query = new WP_Date_Query( $date_query, 'registered' ); // Strip leading 'AND'. $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() ); } $join = ''; $groupby = ''; if ( ! empty( $this->meta_query_clauses ) ) { $join .= $this->meta_query_clauses['join']; // Strip leading 'AND'. $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] ); if ( ! $this->query_vars['count'] ) { $groupby = "{$wpdb->blogs}.blog_id"; } } $where = implode( ' AND ', $this->sql_clauses['where'] ); $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); /** * Filters the site query clauses. * * @since 4.6.0 * * @param string[] $clauses An associative array of site query clauses. * @param WP_Site_Query $query Current instance of WP_Site_Query (passed by reference). */ $clauses = apply_filters_ref_array( 'sites_clauses', array( compact( $pieces ), &$this ) ); $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; if ( $where ) { $where = 'WHERE ' . $where; } if ( $groupby ) { $groupby = 'GROUP BY ' . $groupby; } if ( $orderby ) { $orderby = "ORDER BY $orderby"; } $found_rows = ''; if ( ! $this->query_vars['no_found_rows'] ) { $found_rows = 'SQL_CALC_FOUND_ROWS'; } $this->sql_clauses['select'] = "SELECT $found_rows $fields"; $this->sql_clauses['from'] = "FROM $wpdb->blogs $join"; $this->sql_clauses['groupby'] = $groupby; $this->sql_clauses['orderby'] = $orderby; $this->sql_clauses['limits'] = $limits; // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841. $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}"; if ( $this->query_vars['count'] ) { return (int) $wpdb->get_var( $this->request ); } $site_ids = $wpdb->get_col( $this->request ); return array_map( 'intval', $site_ids ); } /** * Populates found_sites and max_num_pages properties for the current query * if the limit clause was used. * * @since 4.6.0 * * @global wpdb $wpdb WordPress database abstraction object. */ private function set_found_sites() { global $wpdb; if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) { /** * Filters the query used to retrieve found site count. * * @since 4.6.0 * * @param string $found_sites_query SQL query. Default 'SELECT FOUND_ROWS()'. * @param WP_Site_Query $site_query The `WP_Site_Query` instance. */ $found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this ); $this->found_sites = (int) $wpdb->get_var( $found_sites_query ); } } /** * Used internally to generate an SQL string for searching across multiple columns. * * @since 4.6.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param string $search Search string. * @param string[] $columns Array of columns to search. * @return string Search SQL. */ protected function get_search_sql( $search, $columns ) { global $wpdb; if ( str_contains( $search, '*' ) ) { $like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $search ) ) ) . '%'; } else { $like = '%' . $wpdb->esc_like( $search ) . '%'; } $searches = array(); foreach ( $columns as $column ) { $searches[] = $wpdb->prepare( "$column LIKE %s", $like ); } return '(' . implode( ' OR ', $searches ) . ')'; } /** * Parses and sanitizes 'orderby' keys passed to the site query. * * @since 4.6.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param string $orderby Alias for the field to order by. * @return string|false Value to used in the ORDER clause. False otherwise. */ protected function parse_orderby( $orderby ) { global $wpdb; $parsed = false; switch ( $orderby ) { case 'site__in': $site__in = implode( ',', array_map( 'absint', $this->query_vars['site__in'] ) ); $parsed = "FIELD( {$wpdb->blogs}.blog_id, $site__in )"; break; case 'network__in': $network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) ); $parsed = "FIELD( {$wpdb->blogs}.site_id, $network__in )"; break; case 'domain': case 'last_updated': case 'path': case 'registered': case 'deleted': case 'spam': case 'mature': case 'archived': case 'public': $parsed = $orderby; break; case 'network_id': $parsed = 'site_id'; break; case 'domain_length': $parsed = 'CHAR_LENGTH(domain)'; break; case 'path_length': $parsed = 'CHAR_LENGTH(path)'; break; case 'id': $parsed = "{$wpdb->blogs}.blog_id"; break; } if ( ! empty( $parsed ) || empty( $this->meta_query_clauses ) ) { return $parsed; } $meta_clauses = $this->meta_query->get_clauses(); if ( empty( $meta_clauses ) ) { return $parsed; } $primary_meta_query = reset( $meta_clauses ); if ( ! empty( $primary_meta_query['key'] ) && $primary_meta_query['key'] === $orderby ) { $orderby = 'meta_value'; } switch ( $orderby ) { case 'meta_value': if ( ! empty( $primary_meta_query['type'] ) ) { $parsed = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})"; } else { $parsed = "{$primary_meta_query['alias']}.meta_value"; } break; case 'meta_value_num': $parsed = "{$primary_meta_query['alias']}.meta_value+0"; break; default: if ( isset( $meta_clauses[ $orderby ] ) ) { $meta_clause = $meta_clauses[ $orderby ]; $parsed = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})"; } } return $parsed; } /** * Parses an 'order' query variable and cast it to 'ASC' or 'DESC' as necessary. * * @since 4.6.0 * * @param string $order The 'order' query variable. * @return string The sanitized 'order' query variable. */ protected function parse_order( $order ) { if ( ! is_string( $order ) || empty( $order ) ) { return 'ASC'; } if ( 'ASC' === strtoupper( $order ) ) { return 'ASC'; } else { return 'DESC'; } } }